﻿' •———————————————————————————————————————————————————————————————————————•
' | Copyright © 2011 Robert McGinley                                      |
' |                                                                       |
' | This program is free software: you can redistribute it and/or modify  |
' | it under the terms of the GNU General Public License as published by  |
' | the Free Software Foundation, either version 3 of the License, or     |
' | (at your option) any later version.                                   |
' |                                                                       |
' | This program is distributed in the hope that it will be useful,       |
' | but WITHOUT ANY WARRANTY; without even the implied warranty of        |
' | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
' | GNU General Public License for more details.                          |
' |                                                                       |
' | You should have received a copy of the GNU General Public License     |
' | along with this program.  If not, see <http://www.gnu.org/licenses/>. |
' •———————————————————————————————————————————————————————————————————————•
Imports System.IO
Imports SevenZip

Module DecompressionFunctions
    Public Sub ExtractFile(ByVal archivePath As String, ByVal archiveName As String, ByVal extractDir As String)

        ' Set up the required path to 7z.dll from the 7-Zip distribution (Thank you for keeping your code open, Igor. You rule!)
        Dim sevenZipDll As String = My.Computer.FileSystem.CombinePath(AppDomain.CurrentDomain.BaseDirectory.ToString, "\7z.dll")

        ' Get the users temporary directory for us to extract files to
        Dim tempDir As String = Path.GetTempPath()

        Dim parentDir As String = "Scene Unpacker"

        ' Dim archiveFile As String = archivePath + archiveName
        Dim archiveFile As String = My.Computer.FileSystem.CombinePath(archivePath, archiveName)

        ' Make sure the file provided exists
        If Not My.Computer.FileSystem.FileExists(archiveFile) Then
            ErrorMessage("Provided file " + archiveFile + " does not exist", "File does not exist")
        End If

        Try
            SevenZipExtractor.SetLibraryPath(sevenZipDll)
            Dim extractedFile As SevenZipExtractor = New SevenZipExtractor(archiveFile)

            ' Extract the file to the given subdirectory (extractDir) in the users temp directory (tempDir) plus the parentDir
            ' e.g. the full path is generated as follows: tempDir (C:\<temp path from GetTempPath>\) + parentDir (Scene Unpacker\) +
            extractedFile.ExtractArchive(My.Computer.FileSystem.CombinePath(tempDir + "\" + parentDir, extractDir))

        Catch ex As ExtractionFailedException

            ExceptionMessage(ex, "Extraction of file " + archiveFile + " failed")
            Return
        Catch ex As Exception

            ExceptionMessage(ex, "An error has occured", "An error has occured")
            Return
        End Try

        Dim Debug As Boolean = True

        ' DEBUG: Messagebox to confirm extraction was successful
        If Debug = True Then
            MsgBox("DEBUG: Extraction of file " + archiveFile + " to " + My.Computer.FileSystem.CombinePath(tempDir, extractDir).ToString + " successful!")
        End If
    End Sub
End Module
